﻿using MediatR;
using RedDragon.Common.Domain.Process;
using RedDragon.Common.Infrastructure.Interface;
using System.Diagnostics;
using T.Common.Domain.Common;

namespace RedDragon.RBAC.API.Application.Common.ProcessW
{
    public class ProcessDetailCreateHandler : IRequestHandler<ProcessDetailCreateCommon, Result>
    {
        public readonly IBaseRepository<ProcessDetail> ProcessDetailRepository;
        public readonly IBaseRepository<RedDragon.Common.Domain.Process.Process> ProcessRepository;

        public ProcessDetailCreateHandler(IBaseRepository<ProcessDetail> processDetailRepository, IBaseRepository<RedDragon.Common.Domain.Process.Process> processRepository)
        {
            ProcessDetailRepository = processDetailRepository;
            ProcessRepository = processRepository;
        }

        public async Task<Result> Handle(ProcessDetailCreateCommon request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = 0, Msg = "工序明细新增失败" };
            try
            {
                var id = ProcessRepository.Query().Where(x=>x.ProcessNumber==request.ProcessNumber).FirstOrDefault().ProcessId;
                var res = new ProcessDetail()
                {
                    ProcessId = id,
                    ProcessDetailName = request.ProcessDetailName,
                    ProcessDetailUnits = request.ProcessDetailUnits,
                    ProcessDetailDosage = request.ProcessDetailDosage,
                    UnitsPrice = request.UnitsPrice,
                    Summary = request.Summary,
                    CreateTime =DateTime.Now,
                    CreateName="",
                    ProcessDetailState=true
                };
                var res1 = await ProcessDetailRepository.Add(res);
                if (res1 != 0)
                {
                    result.Code = 1;
                    result.Msg = "工序明细新增成功";
                }



            }
            catch (Exception)
            {

                throw;
            }
            return result;
        }
    }
}
